package com.zxw.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

public class DateUtil {

    //计算两个时间相差的秒数
    public static long getDifSeconds(Date startTime, Date endTime) {
        try {
            if (Objects.nonNull(startTime) && Objects.nonNull(endTime)) {
                long sTime = startTime.getTime();
                long eTime = endTime.getTime();
                return (eTime - sTime) / 1000;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static void main(String[] args) throws Exception{
        String s = "2022-10-13 20:35:00";
        Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
        long seconds = getDifSeconds(new Date(), parse);
        System.out.println(seconds);

    }
}
